﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebGioiThieu.Models;

namespace WebGioiThieu.Areas.Admin.Controllers
{
    public class TourManagerController : Controller
    {
        private IntroductionWebDBEntities db = new IntroductionWebDBEntities();

        //
        // GET: /Admin/TourManager/

        //public ViewResult Index(string slug)
        //{
            
        //    return View();
        //}
        //[CustomAuthorize]
        public ViewResult Index(string key,int ? page)
        {
            int total, pageSize = 10;
            int pageNumber = (page ?? 1);
            IEnumerable<Tour> tour;
            if (!string.IsNullOrEmpty(key))
            {
                var tours = (from u in db.Tours
                             where u.TieuDe.Contains(key)
                             orderby u.NgayViet ascending
                             select u);
                tour = tours.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
                total = tours.Count();
            }
            else
            {
                var tours = (from u in db.Tours orderby u.TieuDe ascending select u);
                tour = tours.Skip((pageNumber - 1) * pageSize).Take(pageSize).ToList();
                total = tours.Count();
            }
            var pages = new PageInfo {PageSize = pageSize, TotalItem = total, Url = "/Admin/TourManager/Index", Current = pageNumber };
            ViewBag.Page = pages;
            return View(tour);
        }

        [HttpPost]
        public ActionResult Index(string key,int ? page, FormCollection form)
        {
            var lstValue = form.GetValues("cid");
            if (lstValue != null)
            {
                try
                {
                    foreach (var item in lstValue)
                    {
                        int id = int.Parse(item);
                        Tour tour = db.Tours.Find(id);
                        db.Tours.Remove(tour);
                        db.SaveChanges();
                    }
                }
                catch { 
                    
                }
           
            }
            return RedirectToAction("Index");
        }
        //
        // GET: /Admin/TourManager/Details/5
        [CustomAuthorize]
        public ViewResult Details(int id)
        {
            Tour tour = db.Tours.Find(id);
            return View(tour);
        }

        //
        // GET: /Admin/TourManager/Create
        [CustomAuthorize]
        public ActionResult Create()
        {
            ViewBag.MenuItemID = new SelectList(db.MenuItems, "ID", "Ten");
            return View();
        }

        //
        // POST: /Admin/TourManager/Create
        [CustomAuthorize]
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(Tour tour)
        {
            if (tour.BiDanh == null)
                tour.BiDanh = tour.TieuDe.Replace(" ", "-");
            tour.NgayViet = DateTime.Now;
            tour.NgayCapNhat = DateTime.Now;
            tour.LuotXem = 0;
            tour.MemberID =User.Identity.Name;
            if (ModelState.IsValid)
            {
                db.Tours.Add(tour);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.MenuItemID = new SelectList(db.MenuItems, "ID", "Ten");
            return View(tour);
        }

        //
        // GET: /Admin/TourManager/Edit/5
        [CustomAuthorize]
        public ActionResult Edit(int id)
        {
            Tour tour = db.Tours.Find(id);
            ViewBag.MenuItemID = new SelectList(db.MenuItems,"ID", "Ten",tour.MenuItemID);
            return View(tour);
        }
        //
        // POST: /Admin/TourManager/Edit/5

        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Edit(Tour tour)
        {
            tour.MemberID = User.Identity.Name;
            if (tour.BiDanh == null)
                tour.BiDanh = tour.TieuDe.Replace(" ", "-");
            tour.NgayCapNhat = DateTime.Now;
            if (ModelState.IsValid)
            {
                db.Entry(tour).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.MenuItemID = new SelectList(db.MenuItems, "ID", "Ten", tour.MenuItemID);
            return View(tour);
        }

        //
        // GET: /Admin/TourManager/Delete/5
        [CustomAuthorize]
        public ActionResult Delete(int id)
        {
            Tour tour = db.Tours.Find(id);
            return View(tour);
        }

        //
        // POST: /Admin/TourManager/Delete/5
        [CustomAuthorize]
        [HttpPost, ActionName("Delete")]
        public ActionResult Delete(int id, FormCollection collection)
        {
            Tour tour = db.Tours.Find(id);
            db.Tours.Remove(tour);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}